<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class Adminlog extends Base
{

    public function _initialize()
    {
        parent::_initialize();
        $this->model = 'adminlog';
        $defaultWhere = $this->getDefaultWhere();
        $this->defaultWhere = $defaultWhere;
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $request = $this->request;
        $param = $request->param();
        if ($request->isAjax()){
            $model = model($this->model);
            // 每页起始条数
            $start = ($param['page'] - 1) * $param['limit'];
            // 每页显示条数
            $length = $param['limit'];
            // 排序条件
            $order = '';

            $where = $this->getFilterWhere($request);
            if($this->defaultWhere){
                $where = array_merge($this->defaultWhere, $where);
            }
            if($this->defaultOrder){
                $order = $this->defaultOrder;
            }
            $fields = 'a.log_info,log_time,o.login_name';
            $result = $model->alias('a')
                ->join('operator o','a.admin_id = o.id')
                ->where($where)
                ->limit($start,$length)
                ->order('log_time desc')
                ->field($fields)
                ->select();
            $count = $model->count();
            foreach($result as $k => $v)
            {
                $v['log_time'] = date('Y-m-d H:i:s',$v['log_time']);
                $v['login_name'] = '账号: '.$v['login_name'];
                $v['sort'] = $k + 1;
            }
            $result = [
                'code' => '0',
                'msg' => '请求成功',
                'data' => $result,
                'count' => $count,
            ];

            return json($result);
        }
        return parent::index();
    }
}
